#!/bin/sh
# $Id$

# Installer stub for windows.

# ======================================

appname="$1"
zipname="$2"
startcmd="$3"
arg="$4"
zipurl="$5"
starturl="$6"
# ======================================

VERSION=20090420-1

DIV() {
    echo = -
    echo = --------------------------------------
    echo = -
}
MAIN() {
    stub=installer-stub.cgi
    cat <<INTRO
    echo DOS "$appname" Installer 
-
-
- mini NMT installer - versions cycle around quite fast during beta
- so this is a mini batch file for windows users.
-
- Press any key to continue installing [ "$appname" ] OR press CTRL-C to abort.

INTRO

    echo
    echo   Enter Target NMT Players IP Address:
    read nmtip
    echo
    echo   Enter the ftp password for the NMT. If you dont know it just press enter
    read password
    if [ -z "$password" ] ; then password=1234 ; fi

    set -x
    version="`VERSION $nmtip`"
    urldisk="/`drive_name $nmtip`"

    echo "Found Firmware $version "
    case "$version" in
        *40[0-7]) echo 100 series ; ftpuser=ftpuser ; ftpdisk=/ ;;
        *4??)     echo 200 series ; ftpuser=nmt ; ftpdisk="$urldisk" ;;
    esac

    echo "disk[$urldisk] ftp[$ftpdisk]"
    echo

    if ! wget -q ftp://$ftpuser:$password@$nmtip/ ; then

        echo ERROR unable to connect to $nmtip ftp server with user $ftpuser
        return 1

    fi


    echo = --------------------------------------
    echo  Downloading latest version of "$appname" from "$zipurl"
    if [ -f "$zipname".zip ] ; then rm -f "$zipname".zip ; fi

    wget -O "$zipname".zip "$zipurl" 
    if [ ! -f "$zipname".zip ] ; then
        echo ERROR couldnt get "$zipurl"
        return 1
    fi
        
    DIV

    echo Converting zip file to tar file
    t="delete-me-${zipname}-delete-me"
    if [ -d "$t" ] ; then rm -fr "$t" ; fi
    mkdir "$t"
    cd "$t"
    unzip -qo "../$zipname.zip"
    if [ -f "$zipname.tar" ] ; then
        mv "$zipname.tar" ..
    else
        tar cf "../$zipname.tar" .
    fi
    cd ..
    rm -fr -- "$t"
    if [ ! -f "$zipname.tar" ] ; then
        echo ERROR Converting zip file to tar file
        return 1
    fi
    rm "$zipname.zip"
        
    DIV

    echo = Copying \[ "$zipname".tar \] to PCH

    ftp -n <<HERE  >> install.log
open $nmtip
user $ftpuser $password
bin
cd /Apps/.. # 100 series
cd $ftpdisk
put $zipname.tar
lcd ../helpers
put installer-stub.cgi
quot site chmod 775 installer-stub.cgi
quit
HERE

    DIV

    echo "= Running installer"

    rm -f "$zipname".tar ftp.out || true

    url="http://$nmtip:8883$urldisk/$stub?$appname&$zipname&$startcmd&$arg" 

    echo "= URL = $url"

    wget -q -O - --header="Host: localhost.drives:8883" "$url" 

    DIV

    cat <<HERE1 >index.html
<body>
<h1>$appname</h1>

<h2>TV Access</h2>
<p>To use $appname  via your television go to the web services menu on the TV screen.

<h2>PC Access</h2>
<p>To use on the PC go to <a href="http://$nmtip:8883/$starturl">http://$nmtip:8883/$starturl</a>.
</body>
HERE1

    www-browser index.html 2>/dev/null || firefox index.html 2>/dev/null || cat index.html

    echo = - End
    read x
}


VERSION() {
    wget -q -O - "http://$1:8883/maintenance.html" |\
    sed -rn '
/-[A-Z]+-/ { h } 
$ { x ; s/<[^>]+>//g ; p }'
}
drive_name() {
    wget -O - "http://$1:8883/" --header="Host: localhost.drives:8883" -q  | sed -rn '$ p'
}

MAIN
